package peer.gui;

import common.Packet;

public class UDPNetworkThread extends Thread
{
	private PeerGUI gui;
	
	public UDPNetworkThread(PeerGUI gui)
	{
		this.gui = gui;
	}

	public void run()
	{
		while(true)
		{
			// read in packets
			// create new Packet(string)
			// work out what to do with the packet
			// if packet.type = nw_connect_accept
			// 	  gui.connectionAccepted(true);
			//    respond, cancel timer
			// if packet.type = nw_pong
			//    send packet in response, cancel timer
			// if packet.type = nw_aok
			//    cancel timer which would resend nw_pong, no gui interaction other than gui.writeLog("some message")
			// if packet.type = nw_newpeers_accept
			//    gui.addNewPeers(packet.getHeader("peer_list"));
			//    send nw_newpeers_aok
			// if packet.type = search
			//    if we haven't received this search before
			//        resend packet to all peers in peer list except the one it came from 
			// if packet.type = found
			//    gui.addSearchResult(packet.getHeader("query"), packet.getHeader("filename"), packet.getHeader("size"), packet.getHeader("hashcode"));
			// if packet.type = found_response
			//    cancel timer which would resend found
			// if packet.type = found_response_aok //delete this packet, it has no purpose
			// if packet.type = request_file //delete this packet, it has no purpose
			// if packet.type = request_file_response //delete this packet, it has no purpose
			// if packet.type = request_file_response_aok //delete this packet, it has no purpose
			// if packet.type = status_request
			//    send a reply, gui.addPeer(address, port, 0);
			// if packet.type = status_response
			//    send a reply, cancel a timer
			// if packet.type = status_response_aok
			//    cancel a timer
			// if packet.type = connection_request
			//    send a reply
			// if packet.type = connection_response
			//    send a reply, cancel a timer, start requesting blocks from this host
			// if packet.type = connection_response_aok
			//    cancel a timer
			// if packet.type = request_block
			//    send block
			// and listen for data on another thread (since it'll come via TCP)
		}
	}
	
	public synchronized void send(Packet packet, String address, int port)
	{
		
	}
}
